package com.phpxiu.app.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.phpxiu.app.adapter.DrawRecordListAdapter;
import com.phpxiu.app.config.HttpConfig;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.list.DrawContent;
import com.phpxiu.app.model.response.DrawRecordModel;
import com.phpxiu.app.okhttp.OKHttp;
import com.phpxiu.app.okhttp.handler.OKHttpUIHandler;
import com.phpxiu.app.okhttp.param.OKHttpParam;
import com.phpxiu.app.utils.KKYUtil;
import com.phpxiu.app.view.custom.ListEmptyView;
import com.phpxiu.app.view.custom.WindowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 微信红包领取记录
 * Created by WuJinZhou on 2016/6/5.
 */
public class DrawRecord extends UIBase implements View.OnClickListener{
    public final static String TAG="DrawRecord";
    private View headView;
    private TextView headTitleView;
    private SpannableStringBuilder titleTxt=new SpannableStringBuilder();
    private int colorGreen;
    private float totalValue=123456.15f;
    private ListView mListView;
    private ListEmptyView emptyView;
    private List<DrawContent>records=new ArrayList<DrawContent>();
    private DrawRecordListAdapter mAdapter;
    private WindowLayout mainView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.draw_record);
        mainView=(WindowLayout)findViewById(R.id.root_view);
        findViewById(R.id.top_bar_back_btn).setOnClickListener(this);
        init();
    }

    /**
     * 初始化界面
     */
    private void init(){
        colorGreen=getResources().getColor(R.color.green);
        headView= LayoutInflater.from(this).inflate(R.layout.user_income_draw_record_list_head_view,null);
        headView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, KKYUtil.dip2px(this, 75)));
        headTitleView=(TextView)headView.findViewById(R.id.draw_record_list_view_head_title);
        titleTxt.clear();
        if("zh".equals(getString(R.string.language))){
            titleTxt.append("累计领取：").append(totalValue+"").append("元");
            titleTxt.setSpan(new ForegroundColorSpan(colorGreen),5,titleTxt.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }else{
            titleTxt.append("Total：￥").append(totalValue+"");
            titleTxt.setSpan(new ForegroundColorSpan(colorGreen),7,titleTxt.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        headTitleView.setText(titleTxt);
        emptyView=new ListEmptyView(this);
        emptyView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setMsg(getString(R.string.list_empty_title));
        mListView=(ListView)findViewById(R.id.draw_record_list_view);
        ((ViewGroup) mListView.getParent()).addView(emptyView);
        mListView.setEmptyView(emptyView);
        mListView.setDivider(null);
        //mListView.addHeaderView(headView);
        mAdapter=new DrawRecordListAdapter(this,records);
        mListView.setAdapter(mAdapter);
        request();
    }

    /**
     * 请求数据
     */
    private void request(){
        OKHttpParam param=OKHttpParam.builder(false);
        OKHttp.post(HttpConfig.API_GET_DRAW_RECORD_LIST,param.jsonParam(),TAG,new OKHttpUIHandler(DrawRecordModel.class) {
            @Override
            public void onSuccess(Object model) {
                DrawRecordModel m=(DrawRecordModel)model;
                records.clear();
                emptyView.loaded();
                try{
                    records.addAll(m.getData().getLogList());
                    mAdapter.notifyDataSetChanged();
                }catch (NullPointerException e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onErr(String errMsg) {
                mainView.showTopMsg(errMsg);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_bar_back_btn:
                onBackPressed();
                break;
            default:
                break;
        }
    }
}
